// 手机号的正则
$.validator.addMethod("checkedtel", function (v) {
    var reg = /^1[3-9]\d{9}/
    //v代表验证的值
    if (!reg.test(v)) {
        return false;
    } else {
        return true;
    }
})
//邮箱的验证
$.validator.addMethod("checkedemail", function (v) {
    let reg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
    if (!reg.test(v)) {
        return false;
    } else {
        return true;
    }
})
// 在键盘按下并释放及提交后验证提交表单
$("form").validate({
    rules: {
        username: {
            required: true,
            minlength: 6
        },
        password: {
            required: true,
            minlength: 6
        },
        reqpwd: {
            required: true,
            minlength: 6,
            equalTo: '[name = "password"]'
        },
        email: {
            required: true,
            email: true,
            checkedemail: true

        },
        tel: {
            required: true,
            checkedtel: true,
            maxlength: 11
        }
    },
    messages: {
        username: {
            required: "请输入用户名",
            minlength: "用户名必需由6个字符组成"
        },
        password: {
            required: "请输入密码",
            minlength: "密码长度不能小于 6 个字符"
        },
        reqpwd: {
            required: "请再次输入密码",
            minlength: "密码长度不能小于 6 个字符",
            equalTo: "密码与刚才输入的密码不一致"
        },
        email: "请输入一个正确的邮箱",
        tel: "请输入一个正确的手机号"
    },
    submitHandler: function (f) {
        //禁用提交按钮
        $(this).prop('disabled', true)
        //加载弹出层
        var loadindex = layer.load(2, {
            shade: [0.7, '#fff']
        }, 5000);
        //放松ajax请求
        $.post('../php/register.php', $('form').serialize(), res => {
            var {
                meta: {
                    status,
                    msg
                }
            } = res;
            layer.close(loadindex);
            if (status === 2) {
                layer.msg(msg, {
                    icon: 6,
                    time: 1500
                }, function () {
                    location.href = '../page/signin.html';
                })
            } else {
                $(this).prop('disabled', false);
                layer.msg(msg, {
                    icon: 2,
                    time: 1500
                })
            }

        }, 'json')
    }
});